//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by 张婷 on 15/10/22.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    /// MARK: - 生命周期函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccountModel.avatarUrl)
    }
    
    override func loadView() {
        // 设置根部视图
        view = backImageView
        setupUI()
    }
    
    // 视图已经显示 通常是处理动画或者键盘
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        
        UIView.animateWithDuration(1.2,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 5,
            options: [],
            animations: { () -> Void in
                // 修改所有可动画的属性
                self.view.layoutIfNeeded()
                
            }) { (_) -> Void in
                
                // 标签的动画
                UIView.animateWithDuration(1.2,
                    animations: { () -> Void in
                        self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        
                        // 切换控制器
                       NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootviewControllerNotification, object: nil)
                })
        }
    }
    
    // MARK: - 懒加载控件
    /// 背景图片
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    /// 头像
    private lazy var iconView: UIImageView = {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "avatar_default_big")
        imgView.layer.cornerRadius = 45
        imgView.layer.masksToBounds = true
        
        return imgView
    }()
    /// 欢迎
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎回来", sizeFont: 14)
}

// MARK: - 设置界面
extension WelcomeViewController {
    
    private func setupUI() {
        // 添加子控件
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        welcomeLabel.alpha = 0
        
        // 添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            //make.centerY.equalTo(view.snp_centerY).multipliedBy(0.6)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
}